package com.czy.owner.ui.workorder;

import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import com.czy.owner.R;
import com.czy.owner.adapter.PickCardAdapter;
import com.czy.owner.entity.PickCardEntity;
import com.czy.owner.entity.PrePayBalancePaymentModel;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.PhoneUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by King on 2016/11/29 0029.
 */
public class PickCardActivity extends BaseActivity {
    @BindView(R.id.elv_card)
    ExpandableListView elvCard;
    private PrePayBalancePaymentModel data = null;
    private Map<String, List<PickCardEntity>> dataMap = new HashMap<>();
    private List<String> mapKey = new ArrayList<>();


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_pick_card;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        if (getIntent().getExtras() == null || (data = (PrePayBalancePaymentModel) getIntent().getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY)) == null) {
            PhoneUtils.ShowToastMessage(PickCardActivity.this, "参数异常!");
            finish();
            return;
        }
        initTitle();
        initView();
    }

    @Override
    protected void updateViews() {

    }

    private void initTitle() {
        getToolbarTitle().setText("使用卡项");
        getSubTitle().setText("确定");

    }

    @OnClick(R.id.toolbar_subtitle)
    public void sure(View view){
          /*int pickedCount = 0;
                ArrayList<String> result = new ArrayList<>();
                for (int j = 0; j < mapKey.size(); j++) {
                    for (int i = 0; i < dataMap.get(mapKey.get(j)).size(); i++) {
                        if (dataMap.get(mapKey.get(j)).get(i).isPicked()) {
                            pickedCount++;
                            result.add("" + dataMap.get(mapKey.get(j)).get(i).getUserWalletId());
                        }
                    }
                }*/
        List<PickCardEntity> resultList = new ArrayList<PickCardEntity>();
        ArrayList<String> result = new ArrayList<>();

        for (int j = 0; j < mapKey.size(); j++) {
            for (int i = 0; i < dataMap.get(mapKey.get(j)).size(); i++) {
                if (dataMap.get(mapKey.get(j)).get(i).isPicked()) {
                    resultList.add( dataMap.get(mapKey.get(j)).get(i));
                    result.add("" + dataMap.get(mapKey.get(j)).get(i).getUserWalletId());
                }
            }
        }
//                if (resultList.size() == 0) {
//                    Toast.makeText(PickCardActivity.this, "请至少选择一种卡!", Toast.LENGTH_SHORT).show();
//                } else {
        Intent intent = new Intent();
        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, (Serializable)resultList);
        intent.putStringArrayListExtra("PICK_RESULT",result);
        setResult(RESULT_OK, intent);
        finish();
//                }
    }


    private void initView() {
        for (int i = 0; i < data.getTimeCardList().size(); i++) {
            PickCardEntity mCardEntity = new PickCardEntity(data.getTimeCardList().get(i));
            if (data.getServiceMap().containsKey("" + mCardEntity.getStoreServiceId())) {
                mCardEntity.setPrice(data.getServiceMap().get("" + mCardEntity.getStoreServiceId()));
            }
            if (dataMap.containsKey(mCardEntity.getServiceName())) {
                List<PickCardEntity> dataList = dataMap.get(mCardEntity.getServiceName());
                dataList.add(mCardEntity);
                dataMap.put(mCardEntity.getServiceName(), dataList);
            } else {
                mapKey.add(mCardEntity.getServiceName());
                List<PickCardEntity> dataList = new ArrayList<>();
                dataList.add(mCardEntity);
                dataMap.put(mCardEntity.getServiceName(), dataList);
            }

        }
        for (int i = 0; i < data.getCycleCardList().size(); i++) {
            PickCardEntity mCardEntity = new PickCardEntity(data.getCycleCardList().get(i));
            if (data.getServiceMap().containsKey("" + mCardEntity.getStoreServiceId())) {
                mCardEntity.setPrice(data.getServiceMap().get("" + mCardEntity.getStoreServiceId()));
            }
            if (dataMap.containsKey(mCardEntity.getServiceName())) {
                List<PickCardEntity> dataList = dataMap.get(mCardEntity.getServiceName());
                dataList.add(mCardEntity);
                dataMap.put(mCardEntity.getServiceName(), dataList);
            } else {
                mapKey.add(mCardEntity.getServiceName());
                List<PickCardEntity> dataList = new ArrayList<>();
                dataList.add(mCardEntity);
                dataMap.put(mCardEntity.getServiceName(), dataList);
            }
        }
        for (int i = 0; i < data.getCouponsList().size(); i++) {
            if (data.getCouponsList().get(i).getCouponsType().equals("serviceVoucher")) {
                PickCardEntity mCardEntity = new PickCardEntity(data.getCouponsList().get(i));
                if (data.getServiceMap().containsKey("" + mCardEntity.getStoreServiceId())) {
                    mCardEntity.setPrice(data.getServiceMap().get("" + mCardEntity.getStoreServiceId()));
                }
                if (dataMap.containsKey(mCardEntity.getServiceName())) {
                    List<PickCardEntity> dataList = dataMap.get(mCardEntity.getServiceName());
                    dataList.add(mCardEntity);
                    dataMap.put(mCardEntity.getServiceName(), dataList);
                } else {
                    mapKey.add(mCardEntity.getServiceName());
                    List<PickCardEntity> dataList = new ArrayList<>();
                    dataList.add(mCardEntity);
                    dataMap.put(mCardEntity.getServiceName(), dataList);
                }
            }
        }

//        if (data.getCouponsList().size() != 0) {
            PickCardAdapter mAdapter = new PickCardAdapter(this, dataMap, mapKey);
            elvCard.setAdapter(mAdapter);
            for (int i = 0; i < mAdapter.getGroupCount(); i++) {
                elvCard.expandGroup(i);
            }
            elvCard.setGroupIndicator(null);
            elvCard.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                    return true;
                }
            });

//        }else {
//
//        }

    }


}
